<template>
  <div class="slider-container">
    <button class="slider-button" :class="{ active: isActive }" @click="toggle">
      <span class="slider-text">{{ isActive ? '开' : '关' }}</span>
      <div class="slider-track">
        <div class="slider-thumb" :style="{ left: isActive ? '55%' : '0%' }"></div>
      </div>
    </button>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'

const props = defineProps<{
  status: boolean
}>()
const emit = defineEmits<{
  (e: 'changeStatus', status: boolean): void
}>()

const isActive = ref(props.status)

const toggle = () => {
  isActive.value = !isActive.value
  emit('changeStatus', isActive.value)
}
</script>

<style scoped>
.slider-container {
  width: 30px; /* 根据需要调整宽度 */
  padding: 0px 5px 0px 0px;
}

.slider-button {
  display: inline-block;
  width: 100%;
  height: 14px; /* 根据需要调整高度 */
  background-color: #ffffff;
  border: solid gray 1px;
  border-radius: 30px;
  overflow: hidden;
  position: relative;
  cursor: pointer;
  transition: background-color 0.3s;
}

.slider-button.active {
  background-color: #66ff00;
}

.slider-text {
  position: absolute;
  width: 100%;
  height: 100%;
  line-height: 50px; /* 与按钮高度一致，用于垂直居中 */
  text-align: center;
  color: white;
  font-size: 16px;
  font-weight: bold;
  user-select: none; /* 禁止文本选择 */
}

.slider-track {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
}

.slider-thumb {
  position: absolute;
  top: 0;
  height: 10px;
  width: 10px; /* 滑块宽度，可以是固定的，也可以是百分比 */
  background-color: #bfbfbf;
  margin: 1px;
  border-radius: 25px;
  transition: left 0.3s ease;
}
</style>
